package com.ruoyi.report_gc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_gc.domain.GcqfqdklqddhsclzdlzysljlbBsDetail;
import com.ruoyi.report_gc.domain.vo.GcqfqdklqddhsclzdlzysljlbBsDetailVo;
import com.ruoyi.report_gc.mapper.GcqfqdklqddhsclzdlzysljlbBsDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_gc.domain.bo.GcqfqdklqddhsclzdlzysljlbBsInfoBo;
import com.ruoyi.report_gc.domain.vo.GcqfqdklqddhsclzdlzysljlbBsInfoVo;
import com.ruoyi.report_gc.domain.GcqfqdklqddhsclzdlzysljlbBsInfo;
import com.ruoyi.report_gc.mapper.GcqfqdklqddhsclzdlzysljlbBsInfoMapper;
import com.ruoyi.report_gc.service.IGcqfqdklqddhsclzdlzysljlbBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录Service业务层处理
 *
 * @author llb
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class GcqfqdklqddhsclzdlzysljlbBsInfoServiceImpl implements IGcqfqdklqddhsclzdlzysljlbBsInfoService {

    private final GcqfqdklqddhsclzdlzysljlbBsInfoMapper baseMapper;
    private final GcqfqdklqddhsclzdlzysljlbBsDetailMapper detailMapper;

    /**
     * 查询钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录
     */
    @Override
    @FIllBasicInfo
    public GcqfqdklqddhsclzdlzysljlbBsInfoVo queryById(Long excelId){
        GcqfqdklqddhsclzdlzysljlbBsInfoVo gcqfqdklqddhsclzdlzysljlbBsInfoVo = baseMapper.selectVoById(excelId);
        List<GcqfqdklqddhsclzdlzysljlbBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<GcqfqdklqddhsclzdlzysljlbBsDetail>().eq(GcqfqdklqddhsclzdlzysljlbBsDetail::getExcelId, excelId));
        gcqfqdklqddhsclzdlzysljlbBsInfoVo.setDetailList(detailList);
        return gcqfqdklqddhsclzdlzysljlbBsInfoVo;
    }

    /**
     * 查询钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录列表
     */
    @Override
    public TableDataInfo<GcqfqdklqddhsclzdlzysljlbBsInfoVo> queryPageList(GcqfqdklqddhsclzdlzysljlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GcqfqdklqddhsclzdlzysljlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<GcqfqdklqddhsclzdlzysljlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录列表
     */
    @Override
    public List<GcqfqdklqddhsclzdlzysljlbBsInfoVo> queryList(GcqfqdklqddhsclzdlzysljlbBsInfoBo bo) {
        LambdaQueryWrapper<GcqfqdklqddhsclzdlzysljlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GcqfqdklqddhsclzdlzysljlbBsInfo> buildQueryWrapper(GcqfqdklqddhsclzdlzysljlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GcqfqdklqddhsclzdlzysljlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, GcqfqdklqddhsclzdlzysljlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, GcqfqdklqddhsclzdlzysljlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), GcqfqdklqddhsclzdlzysljlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), GcqfqdklqddhsclzdlzysljlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), GcqfqdklqddhsclzdlzysljlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), GcqfqdklqddhsclzdlzysljlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), GcqfqdklqddhsclzdlzysljlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), GcqfqdklqddhsclzdlzysljlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), GcqfqdklqddhsclzdlzysljlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), GcqfqdklqddhsclzdlzysljlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), GcqfqdklqddhsclzdlzysljlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), GcqfqdklqddhsclzdlzysljlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), GcqfqdklqddhsclzdlzysljlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), GcqfqdklqddhsclzdlzysljlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), GcqfqdklqddhsclzdlzysljlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), GcqfqdklqddhsclzdlzysljlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), GcqfqdklqddhsclzdlzysljlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), GcqfqdklqddhsclzdlzysljlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), GcqfqdklqddhsclzdlzysljlbBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), GcqfqdklqddhsclzdlzysljlbBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), GcqfqdklqddhsclzdlzysljlbBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), GcqfqdklqddhsclzdlzysljlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), GcqfqdklqddhsclzdlzysljlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), GcqfqdklqddhsclzdlzysljlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), GcqfqdklqddhsclzdlzysljlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(GcqfqdklqddhsclzdlzysljlbBsInfoBo bo) {
        GcqfqdklqddhsclzdlzysljlbBsInfo add = BeanUtil.toBean(bo, GcqfqdklqddhsclzdlzysljlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<GcqfqdklqddhsclzdlzysljlbBsDetail> details = new ArrayList<>(6);
            for (int i=1;i<=6;i++ ){
                details.add(new GcqfqdklqddhsclzdlzysljlbBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(GcqfqdklqddhsclzdlzysljlbBsInfoBo bo) {
        GcqfqdklqddhsclzdlzysljlbBsInfo update = BeanUtil.toBean(bo, GcqfqdklqddhsclzdlzysljlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), GcqfqdklqddhsclzdlzysljlbBsDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GcqfqdklqddhsclzdlzysljlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
